﻿
'*************************************************************************
'@Tác giả : Nguyễn Thị Xanh
'@Tên file: FrmDoiMatKhau.vb
'@Ngày tạo: 08/12/2009
'@Ngày sửa: 08/12/2009
'@Mục đích: Đổi mật khẩu tài khoản người sử dụng
'*************************************************************************
Imports System
Imports Microsoft.VisualBasic
Imports System.Linq
Imports System.Windows.Forms
Imports Publics
Public Class FrmDoiMatKhau

    Public change As Boolean = True
     Public Sub ganrong()
        txtmatkhauhientai.Text = ""
        txtmatkhaumoi.Text = ""
        txtmatkhaumoixn.Text = ""
    End Sub
    Private Function CheckSave() As String
        Dim iErrorStr As String = ""
       If Trim(Me.txtmatkhauhientai.Text) = "" Then
            iErrorStr = "Bạn chưa nhập mật khẩu hiện tại"
            txtmatkhauhientai.Focus()
        ElseIf Trim(Me.txtmatkhaumoi.Text) = "" Then
            iErrorStr = "Bạn chưa nhập mật khẩu mới"
            txtmatkhaumoi.Focus()
        ElseIf Trim(Me.txtmatkhaumoixn.Text) = "" Then
            iErrorStr = "Bạn chưa nhập xác nhận mật khẩu mới"
            txtmatkhaumoixn.Focus()
        ElseIf CInt(Trim(Me.txtmatkhaumoi.Text).Length) > 20 Then
            iErrorStr = "Mật khẩu phải nhỏ hơn 20 kí tự"
            txtmatkhaumoi.Focus()
            txtmatkhaumoi.SelectAll()
        End If
        CheckSave = iErrorStr
    End Function
     
    Private Sub FrmDoiMatKhau_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Text = "Đổi mật khẩu"
        lblloimatkhau.Visible = False
    End Sub

    Private Sub BtnGhi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        Dim sthongbao As String = CheckSave()
        Dim matkhauhientai As String = Trim(Me.txtmatkhauhientai.Text)
        If sthongbao <> "" Then
            MsgErr(sthongbao)
            Exit Sub
        End If
        Try
            If Trim(Me.txtmatkhaumoi.Text) = Trim(Me.txtmatkhaumoixn.Text) Then
                Dim user As String = (From ds In dbTV.DM_NhanViens _
                                                Where ds.UserName = vUser.CanBo.UserName _
                                                Select ds.UserName).FirstOrDefault()

                Dim pass As String = (From ds In dbTV.DM_NhanViens _
                                                Where ds.PassWord = Vla.Vms.ClsUtil.MD5Encrypt(matkhauhientai) _
                                                Select ds.PassWord).FirstOrDefault()
                If pass = "" Then
                    lblloimatkhau.Visible = True
                    lblloimatkhau.Text = "Mật khẩu đăng nhập cũ không tồn tại"
                    txtmatkhauhientai.Focus()
                    txtmatkhauhientai.SelectAll()
                    change = False
                    Exit Sub
                End If
                Dim objNSD As DM_NhanVien = (From ds In dbTV.DM_NhanViens _
                                            Where ds.UserName = vUser.CanBo.UserName And ds.PassWord = Vla.Vms.ClsUtil.MD5Encrypt(matkhauhientai) _
                                            Select ds).FirstOrDefault()
                If objNSD IsNot Nothing Then
                    '
                    objNSD.EditedOn = dbTV.GetDateTimeServer.Value

                    objNSD.PassWord = Vla.Vms.ClsUtil.MD5Encrypt(Trim(Me.txtmatkhaumoi.Text))
                    iSubmitChange()
                    '
                    SaveLog("Đổi mật khẩu tài khoản: " & objNSD.UserName, "Đổi mật khẩu", "Mã = " & objNSD.MaSo & "; Tên = " & objNSD.HoTen, "")
                    '
                    MsgErr("Bạn đã đổi mật khẩu thành công")
                    ganrong()
                    txtmatkhauhientai.Focus()
                    Me.Close()
                End If
            Else
                lblloimatkhau.Visible = True
                lblloimatkhau.Text = "Mật khẩu đăng nhập mới nhập không giống nhau."
                txtmatkhaumoi.Focus()
                txtmatkhaumoi.SelectAll()
                change = False
                Exit Sub
            End If
        Catch ex As Exception
            MsgErr(ex.Message)
        End Try
    End Sub


    Private Sub txtmatkhaumoi_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        lblloimatkhau.Visible = False
    End Sub

    Private Sub txtmatkhaumoixn_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        lblloimatkhau.Visible = False
    End Sub

    Private Sub txtmatkhaumoi_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtmatkhaumoi.EditValueChanged
        lblloimatkhau.Visible = False
    End Sub

    Private Sub txttendangnhap_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        lblloimatkhau.Visible = False
    End Sub

    Private Sub txtmatkhauhientai_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtmatkhauhientai.EditValueChanged
        lblloimatkhau.Visible = False
    End Sub

    Private Sub txtmatkhaumoixn_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtmatkhaumoixn.EditValueChanged
        lblloimatkhau.Visible = False
    End Sub

    Private Sub txtmatkhauhientai_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtmatkhauhientai.KeyDown
        If e.KeyCode = Keys.Enter And txtmatkhauhientai.Text <> "" Then
            txtmatkhaumoi.Focus()
        End If
    End Sub

    Private Sub txtmatkhaumoi_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtmatkhaumoi.KeyDown
        If e.KeyCode = Keys.Enter And txtmatkhaumoi.Text <> "" Then
            txtmatkhaumoixn.Focus()
        End If
    End Sub

    Private Sub iCmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles iCmdOK.Click
        BtnGhi_ItemClick(Nothing, Nothing)
    End Sub

    Private Sub iCmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles iCmdCancel.Click
        ganrong()
        Me.Close()
    End Sub

    Private Sub FrmDoiMatKhau_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        'Chấp nhận
        If (e.Modifiers = GetKeyModifide(pIdChon) And _
          e.KeyCode = GetKeyCode2(pIdChon)) Or _
          (e.KeyCode = GetKeyCode(pIdChon)) Then

            If iCmdOK.Enabled = True And iCmdOK.Visible = True Then
                iCmdOK_Click(Nothing, Nothing)
            End If

        End If
        'Huỷ bỏ
        If (e.Modifiers = GetKeyModifide(pIdThoat) And _
          e.KeyCode = GetKeyCode2(pIdThoat)) Or _
          (e.KeyCode = GetKeyCode(pIdThoat)) Then

            If iCmdCancel.Enabled = True And iCmdCancel.Visible = True Then
                iCmdCancel_Click(Nothing, Nothing)
            End If

        End If
    End Sub
End Class